package cn.edu.heuet.bnve.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.grouplist.XUICommonListItemView;

import cn.carbswang.android.numberpickerview.library.NumberPickerView;
import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.model.Schedule;
import cn.edu.heuet.bnve.activity.ScheduleActivity.SetDayDialog;

public class DialogUtil {
    static AlertDialog.Builder dialog;

    private static QMUIDisplayHelper ScreenUtils;
    public static void showDialog(Context context,String msg){
        dialog = new AlertDialog.Builder(context);
        dialog.setTitle("提示");
        dialog.setMessage(msg);
        dialog.setCancelable(false);
        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        dialog.show();
    }

    /**
     * 简单的提示性对话框
     */
    public static void showSimpleTipDialog(Context context,String msg) {
        new MaterialDialog.Builder(context)
                .iconRes(R.drawable.icon_tip)
                .title(R.string.tip_infos)
                .content(msg)
                .positiveText("OK")
                .show();
    }

    /**
     * 带输入框的对话框
     */
    public static void showInputDialog(Context context,String content ,final XUICommonListItemView listItemView,int max,String key) {
        new MaterialDialog.Builder(context)
                .iconRes(R.drawable.ic_checked_right)
                .title("请输入")
                .content(content)
                .inputType(
                        InputType.TYPE_CLASS_TEXT)
                .input(content,"",false,((dialog, input) -> XToastUtils.toast(input.toString())))
                .inputRange(1, max)
                .positiveText(R.string.text_ok)
                .negativeText(R.string.text_cancel)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        listItemView.setDetailText(dialog.getInputEditText().getText().toString());
                        SharepreferencesUtilSystemSettings.putValue(context,key,dialog.getInputEditText().getText().toString());
                    }
                })
                .cancelable(false)
                .show();
    }

    //初始化并弹出对话框
    public static void showSelectDayDialog(final TextView scheduleDay, Context context, final Schedule schedule) {
        final SetDayDialog dialog = new SetDayDialog(context, R.style.dialog_round_top);

        dialog.create();
        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.dialog_style);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = (int) context.getResources().getDisplayMetrics().widthPixels;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        TextView mTextMonthDay = dialog.findViewById(R.id.tv_month_day);
        TextView mTextYear = dialog.findViewById(R.id.tv_year);
        TextView mTextLunar = dialog.findViewById(R.id.tv_lunar);
        Button okButton = dialog.findViewById(R.id.button_ok);
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar calendar = dialog.selectedCalender;
                String day = calendar.getYear()+"/"+calendar.getMonth()+"/"+calendar.getDay();
                scheduleDay.setText(day);
                schedule.setYear(calendar.getYear());
                schedule.setMonth(calendar.getMonth());
                schedule.setDay(calendar.getDay());
                dialog.dismiss();
            }
        });
        Button cancelButton = dialog.findViewById(R.id.button_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        CalendarView mCalendarView =dialog.findViewById(R.id.calendarView);
        mTextYear.setText(String.valueOf(mCalendarView.getCurYear()));
        mTextMonthDay.setText(mCalendarView.getCurMonth() + "月" + mCalendarView.getCurDay() + "日");
        mTextLunar.setText("今日");
        mCalendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                dialog.selectedCalender=calendar;
                TextView mTextMonthDay = dialog.findViewById(R.id.tv_month_day);
                TextView mTextYear = dialog.findViewById(R.id.tv_year);
                TextView mTextLunar = dialog.findViewById(R.id.tv_lunar);
                mTextLunar.setVisibility(View.VISIBLE);
                mTextYear.setVisibility(View.VISIBLE);
                mTextMonthDay.setText(calendar.getMonth() + "月" + calendar.getDay() + "日");
                mTextYear.setText(String.valueOf(calendar.getYear()));
                mTextLunar.setText(calendar.getLunar());

            }
        });
        dialog.show();
    }
    public static void showSelectTimeDialog(final TextView timeTextView,final int i,Context context,final Schedule schedule) {
        final Dialog dialog = new Dialog(context,R.style.dialog_round);
        dialog.create();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_select_time);
        WindowManager.LayoutParams lp = window.getAttributes();
//        window.setWindowAnimations(R.style.activityAnimation);
        lp.gravity = Gravity.CENTER;
        lp.width = ScreenUtils.getScreenWidth(context)/6*5;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        TextView okButton = dialog.findViewById(R.id.button_ok);
        final NumberPickerView hourPicker = dialog.findViewById(R.id.hourPicker);
        final NumberPickerView minPicker = dialog.findViewById(R.id.colorPicker);
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                timeTextView.setText(String.format("%02d", hourPicker.getValue())+":"+String.format("%02d", minPicker.getValue()));
                if (i==0){
                    schedule.setStartTimeHour(hourPicker.getValue());
                    schedule.setStartTimeMinute(minPicker.getValue());
                }
                else{
                    schedule.setEndTimeHour(hourPicker.getValue());
                    schedule.setEndTimeMinute(minPicker.getValue());
                }
                dialog.dismiss();
            }
        });
        TextView cancelButton = dialog.findViewById(R.id.button_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    public static  void showSelectColorDialog(final TextView colorTextView,Context context,final Schedule schedule) {
        final Dialog dialog = new SetDayDialog(context);
        dialog.create();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_select_color);

        WindowManager.LayoutParams lp = window.getAttributes();
//        window.setWindowAnimations(R.style.activityAnimation);
        lp.gravity = Gravity.CENTER;
        lp.width = ScreenUtils.getScreenWidth(context)/6*5;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        final NumberPickerView colorPicker = dialog.findViewById(R.id.colorPicker);
        final FrameLayout colorBlock = dialog.findViewById(R.id.color_block);
        final LinearLayout head = dialog.findViewById(R.id.ll_head);

        colorPicker.setOnValueChangedListener(new NumberPickerView.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
                colorBlock.setBackgroundResource(ColorUtil.getBackgroundResource(newVal));
                head.setBackgroundResource(ColorUtil.getBackgroundColor(newVal));
                schedule.setAlarmColor(newVal);
            }
        });

        TextView okButton = dialog.findViewById(R.id.button_ok);

        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                colorTextView.setText(ColorUtil.getColorName(colorPicker.getValue()));
                dialog.dismiss();
            }
        });
        TextView cancelButton = dialog.findViewById(R.id.button_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }
}
